const Router = require("koa-router");
const bodyParser = require("koa-bodyparser");
const path = require("path");
const article = require("../utils/sql");
const network = new Router();
const fs = require("fs");
network.use(bodyParser());

// 获取所有network的数据
network.post("/fileList", async (ctx) => {
  const { user_id } = ctx.request.body;
  const data = await article.getFile(user_id);
  const filterData = data.data.filter((item) => item.recycle === 0);
  ctx.body = filterData;
});

// 回收站
network.post("/recycle", async (ctx) => {
  const { userId } = ctx.request.body;
  const data = await article.getRecycle(userId);
  ctx.body = data;
});

// 搜索
network.post("/search", async (ctx) => {
  const { params } = ctx.request.body;
  const keyword = params;
  const data = await article.search(keyword);
  ctx.body = data;
});

// 预览文件
network.get("/preview", async (ctx) => {
  const { id } = ctx.query;
  const data = await article.previewFile(id);
  const { url } = data[0];
  const filePath = path.join(__dirname, ".." + url);
  const fileName = path.basename(filePath);
  ctx.attachment(fileName);
});

// 删除文件deleteAsid
network.post("/delete", async (ctx) => {
  const { id } = ctx.request.body;
  const data = await article.deleteAsid(id);
  ctx.body = data;
});

// 永久删除文件
network.post("/deleteForever/:username", async (ctx) => {
  const { id } = ctx.request.body;
  const username = ctx.params.username;
  // const file = await article.getFileById(id);

  const data = await article.deleteForever(id);
  ctx.body = data;
});

// 恢复文件
network.post("/recover", async (ctx) => {
  const { fileId } = ctx.request.body;
  const data = await article.recoverFile(fileId);
  ctx.body = data;
});

// 移动文件
network.post("/move", async (ctx) => {
  // { id: 319, folder_id: '24' }
  const { id, folder_id } = ctx.request.body;
  const data = await article.moveFile(id, folder_id);
  ctx.body = data;
});

// 创建文件分享
network.post("/share", async (ctx) => {
  const { fileId, sharePassword, expireHours, maxAccessCount } = ctx.request.body;
  const userId = ctx.state.user.id; // 从JWT中获取用户ID

  try {
    const data = await article.shareFile(fileId, userId, sharePassword, expireHours, maxAccessCount);
    if (data.success) {
      ctx.body = {
        code: 200,
        msg: "分享创建成功",
        data: data.data,
      };
    } else {
      ctx.status = 400;
      ctx.body = {
        code: 400,
        msg: data.message,
      };
    }
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      msg: "服务器错误",
    };
  }
});

// 取消分享
network.post("/cancelShare", async (ctx) => {
  const { shareId } = ctx.request.body;
  try {
    const data = await article.cancelShareFile(shareId);
    if (data.success) {
      ctx.body = {
        code: 200,
        msg: data.data,
      };
    } else {
      ctx.status = 400;
      ctx.body = {
        code: 400,
        msg: data.message,
      };
    }
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      msg: "服务器错误",
    };
  }
});

// 获取用户的分享列表
network.post("/myShares", async (ctx) => {
  const userId = ctx.state.user.id; // 从JWT中获取用户ID

  try {
    const data = await article.getUserShares(userId);
    if (data.success) {
      ctx.body = {
        code: 200,
        data: data.data,
      };
    } else {
      ctx.status = 400;
      ctx.body = {
        code: 400,
        msg: data.message,
      };
    }
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      msg: "服务器错误",
    };
  }
});

// 通过分享码访问分享（不需要JWT验证）
network.get("/share/:shareCode", async (ctx) => {
  const { shareCode } = ctx.params;
  const { password } = ctx.query;

  try {
    const data = await article.accessShare(shareCode, password);
    if (data.success) {
      ctx.body = {
        code: 200,
        data: data.data,
      };
    } else {
      ctx.status = 400;
      ctx.body = {
        code: 400,
        msg: data.message,
      };
    }
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      msg: "服务器错误",
    };
  }
});

// 容量统计
network.post("/memory", async (ctx) => {
  const { id } = ctx.request.body;
  const data = await article.memory(id);
  ctx.body = data;
});

module.exports = network;
